﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using RM2012Service.Business;
using System.Data;
using RM2012Service.DataAccess;

namespace RM2012Service.Controller
{
    public class UsersCtrl
    {
        UsersDAO dao;

        public UsersCtrl()
        {
            dao = new UsersDAO();
        }

        public UsersInfo[] GetAllUsers()
        {
            UsersInfo[] list = null;
            try
            {
                DataTable dt = dao.GetAllUsers();
                if (dt.Rows.Count > 0)
                {
                    list = new UsersInfo[dt.Rows.Count];
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        list[i] = new UsersInfo(dt.Rows[i]["UserId"].ToString(),
                            dt.Rows[i]["UserName"].ToString(),
                            dt.Rows[i]["PassWord"].ToString(),
                            dt.Rows[i]["FullName"].ToString(),
                            dt.Rows[i]["Salary"].ToString(),
                            dt.Rows[i]["Address"].ToString(),
                            dt.Rows[i]["Phone"].ToString(),
                            dt.Rows[i]["Email"].ToString(),
                            dt.Rows[i]["Sex"].ToString(),
                            dt.Rows[i]["Remark"].ToString());
                    }
                }
            }
            catch
            {
                list = null;
            }
            return list;
        }

        public UsersInfo GetUserById(string userId)
        {
            UsersInfo info = null;
            try
            {
                DataTable dt = dao.GetUserById(userId);
                if (dt.Rows.Count > 0)
                {
                    info = new UsersInfo(dt.Rows[0]["UserId"].ToString(),
                            dt.Rows[0]["UserName"].ToString(),
                            dt.Rows[0]["PassWord"].ToString(),
                            dt.Rows[0]["FullName"].ToString(),
                            dt.Rows[0]["Salary"].ToString(),
                            dt.Rows[0]["Address"].ToString(),
                            dt.Rows[0]["Phone"].ToString(),
                            dt.Rows[0]["Email"].ToString(),
                            dt.Rows[0]["Sex"].ToString(),
                            dt.Rows[0]["Remark"].ToString());
                }
            }
            catch
            {
                info = null;
            }
            return info;
        }

        public UsersInfo GetUserByUserName(string userName)
        {
            UsersInfo info = null;
            try
            {
                DataTable dt = dao.GetUserByUserName(userName);
                if (dt.Rows.Count > 0)
                {
                    info = new UsersInfo(dt.Rows[0]["UserId"].ToString(),
                            dt.Rows[0]["UserName"].ToString(),
                            dt.Rows[0]["PassWord"].ToString(),
                            dt.Rows[0]["FullName"].ToString(),
                            dt.Rows[0]["Salary"].ToString(),
                            dt.Rows[0]["Address"].ToString(),
                            dt.Rows[0]["Phone"].ToString(),
                            dt.Rows[0]["Email"].ToString(),
                            dt.Rows[0]["Sex"].ToString(),
                            dt.Rows[0]["Remark"].ToString());
                }
            }
            catch
            {
                info = null;
            }
            return info;
        }

        public UsersInfo[] GetUserByFullName(string fullName)
        {
            UsersInfo[] list = null;
            try
            {
                DataTable dt = dao.GetUserByFullName(fullName);
                if (dt.Rows.Count > 0)
                {
                    list = new UsersInfo[dt.Rows.Count];
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        list[i] = new UsersInfo(dt.Rows[i]["UserId"].ToString(),
                            dt.Rows[i]["UserName"].ToString(),
                            dt.Rows[i]["PassWord"].ToString(),
                            dt.Rows[i]["FullName"].ToString(),
                            dt.Rows[i]["Salary"].ToString(),
                            dt.Rows[i]["Address"].ToString(),
                            dt.Rows[i]["Phone"].ToString(),
                            dt.Rows[i]["Email"].ToString(),
                            dt.Rows[i]["Sex"].ToString(),
                            dt.Rows[i]["Remark"].ToString());
                    }
                }
            }
            catch
            {
                list = null;
            }
            return list;
        }

        public UsersInfo[] GetUserByAddress(string address)
        {
            UsersInfo[] list = null;
            try
            {
                DataTable dt = dao.GetUserByAddress(address);
                if (dt.Rows.Count > 0)
                {
                    list = new UsersInfo[dt.Rows.Count];
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        list[i] = new UsersInfo(dt.Rows[i]["UserId"].ToString(),
                            dt.Rows[i]["UserName"].ToString(),
                            dt.Rows[i]["PassWord"].ToString(),
                            dt.Rows[i]["FullName"].ToString(),
                            dt.Rows[i]["Salary"].ToString(),
                            dt.Rows[i]["Address"].ToString(),
                            dt.Rows[i]["Phone"].ToString(),
                            dt.Rows[i]["Email"].ToString(),
                            dt.Rows[i]["Sex"].ToString(),
                            dt.Rows[i]["Remark"].ToString());
                    }
                }
            }
            catch
            {
                list = null;
            }
            return list;
        }

        public UsersInfo[] GetUserByPhone(string phone)
        {
            UsersInfo[] list = null;
            try
            {
                DataTable dt = dao.GetUserByPhone(phone);
                if (dt.Rows.Count > 0)
                {
                    list = new UsersInfo[dt.Rows.Count];
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        list[i] = new UsersInfo(dt.Rows[i]["UserId"].ToString(),
                            dt.Rows[i]["UserName"].ToString(),
                            dt.Rows[i]["PassWord"].ToString(),
                            dt.Rows[i]["FullName"].ToString(),
                            dt.Rows[i]["Salary"].ToString(),
                            dt.Rows[i]["Address"].ToString(),
                            dt.Rows[i]["Phone"].ToString(),
                            dt.Rows[i]["Email"].ToString(),
                            dt.Rows[i]["Sex"].ToString(),
                            dt.Rows[i]["Remark"].ToString());
                    }
                }
            }
            catch
            {
                list = null;
            }
            return list;
        }

        public UsersInfo[] GetUserByEmail(string email)
        {
            UsersInfo[] list = null;
            try
            {
                DataTable dt = dao.GetUserByEmail(email);
                if (dt.Rows.Count > 0)
                {
                    list = new UsersInfo[dt.Rows.Count];
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        list[i] = new UsersInfo(dt.Rows[i]["UserId"].ToString(),
                            dt.Rows[i]["UserName"].ToString(),
                            dt.Rows[i]["PassWord"].ToString(),
                            dt.Rows[i]["FullName"].ToString(),
                            dt.Rows[i]["Salary"].ToString(),
                            dt.Rows[i]["Address"].ToString(),
                            dt.Rows[i]["Phone"].ToString(),
                            dt.Rows[i]["Email"].ToString(),
                            dt.Rows[i]["Sex"].ToString(),
                            dt.Rows[i]["Remark"].ToString());
                    }
                }
            }
            catch
            {
                list = null;
            }
            return list;
        }

        public UsersInfo[] GetUserByGroup(string groupId)
        {
            UsersInfo[] list = null;
            try
            {
                DataTable dt = dao.GetUserByGroup(groupId);
                if (dt.Rows.Count > 0)
                {
                    list = new UsersInfo[dt.Rows.Count];
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        list[i] = new UsersInfo(dt.Rows[i]["UserId"].ToString(),
                            dt.Rows[i]["UserName"].ToString(),
                            dt.Rows[i]["PassWord"].ToString(),
                            dt.Rows[i]["FullName"].ToString(),
                            dt.Rows[i]["Salary"].ToString(),
                            dt.Rows[i]["Address"].ToString(),
                            dt.Rows[i]["Phone"].ToString(),
                            dt.Rows[i]["Email"].ToString(),
                            dt.Rows[i]["Sex"].ToString(),
                            dt.Rows[i]["Remark"].ToString());
                    }
                }
            }
            catch
            {
                list = null;
            }
            return list;
        }

        public bool UserHasGroup(string userId)
        {
            int bHas = Convert.ToInt32(dao.UserHasGroup(userId));
            if (bHas > 0)
            {
                return true;
            } 
            else
            {
                return false;
            }
        }

        public bool ExistUserByUserName(string userName)
        {
            int bExist = Convert.ToInt32(dao.ExistUserByUserName(userName));
            if (bExist > 0)
            {
                return true;
            } 
            else
            {
                return false;
            }
        }

        public bool ExistUserByUserNameAndPassword(string userName, string passWord)
        {
            int bExist = Convert.ToInt32(dao.ExistUserByUserNameAndPassword(userName, passWord));
            if (bExist > 0)
            {
                return true;
            } 
            else
            {
                return false;
            }
        }

        public UserGroupInfo GetUserGroup(string userId)
        {
            UserGroupInfo info = null;
            try
            {
                DataTable dt = dao.GetUserGroup(userId);
                if (dt.Rows.Count > 0)
                {
                    info = new UserGroupInfo(dt.Rows[0]["UserGroupId"].ToString(),
                            dt.Rows[0]["UserGroupName"].ToString(),
                            (bool)dt.Rows[0]["IsDefault"],
                            (bool)dt.Rows[0]["IsAdmin"],
                            (bool)dt.Rows[0]["IsMod"],
                            (bool)dt.Rows[0]["CanDelete"],
                            (bool)dt.Rows[0]["CanReport"],
                            (bool)dt.Rows[0]["CanEdit"],
                            dt.Rows[0]["Remark"].ToString());
                }
            }
            catch
            {
                info = null;
            }
            return info;
        }

        public bool AddUser(UsersInfo info)
        {
            int bAdd = dao.AddUser(info);
            if (bAdd > 0)
            {
                return true;
            } 
            else
            {
                return false;
            }
        }

        public bool UpdateUser(UsersInfo info)
        {
            int bUpdate = dao.UpdateUser(info);
            if (bUpdate > 0)
            {
                return true;
            } 
            else
            {
                return false;
            }
        }

        public bool UpdateUserNoPass(UsersInfo info)
        {
            int bUpdateNoPass = dao.UpdateUserNoPass(info);
            if (bUpdateNoPass > 0)
            {
                return true;
            } 
            else
            {
                return false;
            }
        }

        public bool UpdateProfile(UsersInfo info)
        {
            int bUpdateProfile = dao.UpdateProfile(info);
            if (bUpdateProfile > 0)
            {
                return true;
            } 
            else
            {
                return false;
            }
        }

        public bool ChangePassword(string userId, string passWord)
        {
            int bChangePass = dao.ChangePassword(userId, passWord);
            if (bChangePass > 0)
            {
                return true;
            } 
            else
            {
                return false;
            }
        }

        public string GetIdByUsername(string userName)
        {
            return dao.GetIdByUsername(userName).ToString();
        }

        public bool DeleteUser(string userId)
        {
            int bDelete = dao.DeleteUser(userId);
            if (bDelete > 0)
            {
                return true;
            } 
            else
            {
                return false;
            }
        }
    }
}
